package utils

import (
	"fmt"
	"strings"
	"unicode"
)

// CamelToSnake 驼峰转下划线
func CamelToSnake(str string) string {
	var result strings.Builder
	for i, char := range str {
		if unicode.IsUpper(char) && unicode.IsLetter(char) {
			if i > 0 {
				result.WriteRune('_')
			}
			result.WriteRune(unicode.ToLower(char))
		} else {
			result.WriteRune(char)
		}
	}
	return result.String()
}

// SnakeToCamel 下划线转驼峰
func SnakeToCamel(str string) string {
	newArr := make([]string, 0)
	strArr := strings.Split(str, "_")
	for i, char := range strArr {
		if i == 0 {
			newArr = append(newArr, char)
		} else {
			char = strings.ToUpper(char[:1]) + char[1:]
			newArr = append(newArr, char)
		}
	}
	return strings.Join(newArr, "")
}

// SnakeToCamelUc 下划线转驼峰(首字母大写)
func SnakeToCamelUc(str string) string {
	newArr := make([]string, 0)
	strArr := strings.Split(str, "_")
	for _, char := range strArr {
		char = strings.ToUpper(char[:1]) + char[1:]
		newArr = append(newArr, char)
	}
	return strings.Join(newArr, "")
}

func StrTest() {
	str := "system_user_role"
	str2 := "systemUserRole"
	str3 := "SystemUserRole"
	fmt.Println("SnakeToCamelUc===" + str + ":" + SnakeToCamelUc(str))
	fmt.Println("SnakeToCamel===" + str + ":" + SnakeToCamel(str))
	fmt.Println("CamelToSnake===" + str2 + ":" + CamelToSnake(str2))
	fmt.Println("CamelToSnake===" + str3 + ":" + CamelToSnake(str3))
}
